/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TagLibrary;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

public class GetTag extends TagSupport {

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int doStartTag() throws JspException {
        Stack stack = (Stack) this.pageContext.getAttribute(PageParameter.AttributeUtil.templeStack, 
                PageContext.REQUEST_SCOPE);
        if (stack == null) {
            throw new JspException("GetTag.doStartTag(): NO STACK");
        }
        Hashtable params = (Hashtable) stack.peek();
        if (params == null) {
            throw new JspException("GetTag.doStartTag(): NO HASHTABLE");
        }
        PageParameter param = (PageParameter) params.get(name);
        if (param != null) {
            JspWriter out = this.pageContext.getOut();
            try {
                if (param.getDirect() == true) {
                    out.println(param.getContent());
                } else {
                    out.flush();
                    this.pageContext.include(param.getContent());
                }
            } catch (ServletException ex) {
                Logger.getLogger(GetTag.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(GetTag.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return SKIP_BODY;
    }
}